<template>
<div class="my-favorites abs full">
	<div class="rel full overflow-x-hidden" :style="{'-webkit-overflow-scrolling': scrollMode}">
		<mt-loadmore :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" :auto-fill="false" ref="loadmore">
			<search-result v-for="item in myFavoritesList" :isValid="item.isValid" :key="item.id" :detail="item.insurance" @click.native="goDetail(item.insurance.ID)" @cancelFavorites="cancelFavorit"></search-result>
		</mt-loadmore>
		<div v-if="myFavoritesList.length==0" class="none-info">您没有收藏~</div>
	</div>
</div>
</template>

<script>
import SearchResult from '../../components/search-result/search-result.vue'
export default {
	name: 'my-favorites',
	components: {
		SearchResult
	},
	data() {
		return {
			pageStart: 1,
			allLoaded: false, //是否可以上拉属性，false可以上拉，true为禁止上拉
			scrollMode: "auto", //移动端弹性滚动效果，touch为弹性滚动，auto是非弹性滚动  
			myFavoritesList: [] //我的收藏list
		}
	},
	mounted() {
		// 为方便调试把this暴露在Vue原型链下
		this.$pages.myFavorites = this;
	},
	activated() {
		document.title = '我的收藏';
		this.postMyFavorites();
	},
	deactivated() {
		this.pageStart = 1;
		this.allLoaded = false;
		this.myFavoritesList = [];
	},
	methods: {
		goDetail(id) {
			this.$router.push('/detail?id=' + id);
		},
		// 上拉加载更多
		loadBottom: function() {
			this.pageStart++;
			this.postMyFavorites(this.pageStart);
			this.$refs.loadmore.onBottomLoaded(); // 固定方法，查询完要调用一次，用于重新定位  
		},
		// 请求数据，初始化pageStart=1
		postMyFavorites(pageStart = 1) {
			let that = this;
			// 获取用户ID
			let customerId = this.$util.getCustomerId();
			if (!customerId) {
				return;
			}
			this.$util.ajax(`/insurance/collection/my`, 'post', {
					customerId: customerId,
					pageStart: pageStart,
					pageSize: 5
				})
				.then(function(data) {
					console.log('我的收藏');
					console.log(data);
					if (data.code == "100000") {
						data.data.list.length < 5 ? that.allLoaded = true : ''; //数据量小于pageSize,不能执行上拉加载更多操作
						data.data.list.forEach(function(item) {
							if (item.insurance) {
								that.myFavoritesList = that.myFavoritesList.concat(item);
							}
						});
					}
				}).catch(function(err) {
					console.log(err);
				});
		},
		// 点击取消收藏，删除列表内容
		cancelFavorit(insuranceId) {
			let index, that = this;
			this.myFavoritesList.forEach(function(item) {
				if (item.insurance.ID == insuranceId) {
					index = that.myFavoritesList.indexOf(item);
				}
			});
			this.myFavoritesList.splice(index, 1);
		}
	}
}
</script>

<style scoped>
.my-favorites {
	background: url(../detail/image/star_bg.png) no-repeat;
	background-size: 100%;
}

.result-item:last-child {
	margin-bottom: 1rem
}

.result-item:first-child {
	margin-top: .35rem
}

.mint-loadmore-text {
	font-size: .3rem;
}

.mint-loadmore-bottom {
	display: none!important;
}

.none-info {
	text-align: center;
	margin-top: 50%;
	font-size: .8rem;
	color: #FBFBFC;
}
</style>
